package com.wangjiang.foundation.common.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;


public class DateUtil {

    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String YYYY_MM = "yyyy-MM";

    public static String getCurrentDateString(String fmt) {
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        return sdf.format(new Date());
    }

    public static String getCurrentDateString() {
        return getCurrentDateString(YYYY_MM_DD);
    }

    /**
     * 日期转字符串
     *
     * @param date
     * @return
     */
    public static String getTimeString(Date date) {
        if (null == date) {
            return "";
        } else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return simpleDateFormat.format(date);
        }
    }

    public static String getTime(Date date) {
        if (null == date) {
            return "";
        } else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD);
            return simpleDateFormat.format(date);
        }
    }

    /**
     * 日期转字符串 yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String getTimeStringShort(Date date) {
        if (null == date) {
            return "";
        } else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            return simpleDateFormat.format(date);
        }
    }

    /**
     * @Author: huZhiHao
     * @Description: 日期转自定义字符串
     * @Date: 2020/6/5
     * @Params: [date, fmt]
     * @Return: java.lang.String
     **/
    public static String getTimeStringCustom(Date date, String fmt) {

        if (null == date) {
            return "";
        } else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(fmt);
            return simpleDateFormat.format(date);
        }
    }

    /**
     * 字符串转时间转换日期格式
     *
     * @param date    时间字符串
     * @param pattern 字符串时间格式
     * @return
     * @throws ParseException
     */
    public static Date parseTimeStringToDate(String date, String pattern) throws ParseException {
        if (CommonUtils.isNull(date) || StringUtil.trim(date).equals("")) {
            return null;
        } else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
            return simpleDateFormat.parse(date);
        }
    }

    public static long getDay(String pay_time, Integer cycle) throws ParseException {
        Date date = DateUtil.parseTimeStringToDate(pay_time, DateUtil.YYYY_MM_DD);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -cycle);
        return (date.getTime() - calendar.getTimeInMillis()) / (24 * 60 * 60 * 1000);
    }

    public static Boolean isGenerate(String pay_time, Integer remind) throws ParseException {
        if (null == remind) {
            remind = 5;
        }
        Date date = DateUtil.parseTimeStringToDate(pay_time, DateUtil.YYYY_MM_DD);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -remind);
        if (calendar.getTimeInMillis() <= new Date().getTime()) {
            return true;
        }
        return false;
    }

    /**
     * 2017-08-19 20:30
     *
     * @return
     */
    public static String[] getDate(String time) {
        String[] strs = time.split(":");
        return strs;
    }

    /**
     * 日期格式字符串转换成时间戳
     *
     * @param date_str 字符串日期
     * @param format   如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static Long date2TimeStamp(String date_str, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(date_str).getTime() / 1000;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 时间戳转换成日期格式字符串
     *
     * @param seconds 精确到秒的字符串
     * @return
     */
    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds + "000")));
    }

    public static String getPayTime(Integer payment_cycle, String contract_start_date) throws ParseException {
        Calendar plan = Calendar.getInstance();
        plan.setTime(parseTimeStringToDate(contract_start_date, YYYY_MM_DD));
        plan.add(Calendar.MONTH, payment_cycle);
        return getTimeStringShort(plan.getTime());
    }

    public static String AddMonth(Integer month, String date) throws ParseException {
        Calendar plan = Calendar.getInstance();
        plan.setTime(parseTimeStringToDate(date, YYYY_MM_DD));
        plan.add(Calendar.MONTH, month);
        return getTimeStringShort(plan.getTime());
    }

    public static String getCurrentMonth() throws ParseException {
        Calendar plan = Calendar.getInstance();
        plan.setTime(new Date());
        return String.valueOf(plan.get(Calendar.MONTH) + 1);
    }

    public static String getCurrentYear() throws ParseException {
        Calendar plan = Calendar.getInstance();
        plan.setTime(new Date());
        return String.valueOf(plan.get(Calendar.YEAR));
    }


    public static String getMonthStart(String date) throws ParseException {
        Calendar plan = Calendar.getInstance();
        plan.setTime(parseTimeStringToDate(date, YYYY_MM));
        plan.add(Calendar.MONTH, 0);
        plan.set(Calendar.DAY_OF_MONTH, 1);
        return getTime(plan.getTime());
    }

    public static String getMonthEnd(String date) throws ParseException {
        Calendar plan = Calendar.getInstance();
        plan.setTime(parseTimeStringToDate(date, YYYY_MM));
        plan.add(Calendar.MONTH, 1);
        plan.set(Calendar.DAY_OF_MONTH, 0);
        return getTime(plan.getTime());
    }

    public static Boolean compareTo(String time1, String time2) throws ParseException {
        Date date1 = DateUtil.parseTimeStringToDate(time1, YYYY_MM_DD);
        Date date2 = DateUtil.parseTimeStringToDate(time2, YYYY_MM_DD);

        if (date1.getTime() - date2.getTime() >= 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取某日期是一年的第几个星期
     */
    public static Integer getWeekOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(date);
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 获取某个日期的星期一是哪天
     */
    public static Date getWeekStartFromDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return calendar.getTime();
    }

    /**
     * 获取某个日期的星期天是哪天
     */
    public static Date getWeekEndFromDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getWeekStartFromDate(date));
        calendar.add(Calendar.DAY_OF_WEEK, 6);
        return calendar.getTime();
    }


    /**
     * 两个日期之间的天数
     *
     * @param date1
     * @param date2
     * @return
     * @throws ParseException
     */
    public static int getDateSpace(String date1, String date2)
            throws ParseException {

        Calendar calst = Calendar.getInstance();
        ;
        Calendar caled = Calendar.getInstance();

        calst.setTime(DateUtil.parseTimeStringToDate(date1, YYYY_MM_DD));
        caled.setTime(DateUtil.parseTimeStringToDate(date2, YYYY_MM_DD));

        //设置时间为0时
        calst.set(Calendar.HOUR_OF_DAY, 0);
        calst.set(Calendar.MINUTE, 0);
        calst.set(Calendar.SECOND, 0);
        caled.set(Calendar.HOUR_OF_DAY, 0);
        caled.set(Calendar.MINUTE, 0);
        caled.set(Calendar.SECOND, 0);
        //得到两个日期相差的天数   
        int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;

        return days;
    }

    //获取审核剩余时间 审核时间5天
    public static String getOverTimeStr(String create_time, Integer overDay) throws ParseException {
        String overTimeStr = "";
        if (null == overDay) {
            overDay = 5;
        }
        if (CommonUtils.isNotNull(create_time)) {
            Calendar overTime = Calendar.getInstance();
            Calendar now = Calendar.getInstance();
            overTime.setTime(DateUtil.parseTimeStringToDate(create_time, "yyyy-MM-dd HH:mm:ss"));
            overTime.add(Calendar.DATE, overDay);
            now.setTime(new Date());

            overTime.set(Calendar.MINUTE, 0);
            overTime.set(Calendar.SECOND, 0);
            now.set(Calendar.MINUTE, 0);
            now.set(Calendar.SECOND, 0);

            //得到两个日期相差的小时数
            int hours = ((int) (overTime.getTime().getTime() / 1000) - (int) (now.getTime().getTime() / 1000)) / 3600;
            if (hours / 24 > 0) {
                overTimeStr = "剩余" + String.valueOf((int) Math.floor(hours / 24)) + "个工作日";//+(hours%24)+"小时";
            } else {
                overTimeStr = "超出" + String.valueOf((-1) * (int) Math.floor(hours / 24)) + "个工作日";//+(hours%24)+"小时";
            }
        }
        return overTimeStr;
    }

    //获取审核处理时间 审核时间5天
    public static String getDealTimeStr(String create_time, String deal_time, Integer overDay) throws ParseException {
        String overTimeStr = "";
        if (null == overDay) {
            overDay = 5;
        }
        if (CommonUtils.isNotNull(create_time) && CommonUtils.isNotNull(deal_time)) {
            Calendar overTime = Calendar.getInstance();
            Calendar dealTime = Calendar.getInstance();
            overTime.setTime(DateUtil.parseTimeStringToDate(create_time, "yyyy-MM-dd HH:mm:ss"));
            overTime.add(Calendar.DATE, overDay);
            dealTime.setTime(DateUtil.parseTimeStringToDate(deal_time, "yyyy-MM-dd HH:mm:ss"));

            overTime.set(Calendar.MINUTE, 0);
            overTime.set(Calendar.SECOND, 0);
            dealTime.set(Calendar.MINUTE, 0);
            dealTime.set(Calendar.SECOND, 0);

            //得到两个日期相差的小时数
            int hours = ((int) (overTime.getTime().getTime() / 1000) - (int) (dealTime.getTime().getTime() / 1000)) / 3600;
            if (hours / 24 > 0) {
                overTimeStr = "提前" + String.valueOf((int) Math.floor(hours / 24)) + "个工作日完成";//+(hours%24)+"小时完成";
            } else {
                overTimeStr = "超出" + String.valueOf((-1) * (int) Math.floor(hours / 24)) + "个工作日完成";//+(hours%24)+"小时完成";
            }
        }

        return overTimeStr;
    }

    //5秒内不能重复同一请求
    public static boolean isFrequent(String time) throws ParseException {
        Date preTime = DateUtil.parseTimeStringToDate(time, "yyyy-MM-dd HH:mm:ss");
        Date currTime = new Date();
        if (currTime.getTime() - preTime.getTime() < 5000) {
            return true;
        }
        return false;
    }

    //	获取当前时间
    public static Date getNowDate() {
        TimeZone time = TimeZone.getTimeZone("Asia/Shanghai");
//        TimeZone time = TimeZone.getTimeZone("GMT+:16:00");
        TimeZone.setDefault(time);
        Date now = new Date();
        return now;
    }

    //	获取当天日期的最小
    public static String getDayStart(String datetime) throws ParseException {
        if (CommonUtils.isNull(datetime)) {
            return null;
        }
        Date date = parseTimeStringToDate(datetime.substring(0, 10), YYYY_MM_DD);
        String dayStart = datetime.substring(0, 10) + " 00:00:00";
        return dayStart;
    }

    //	获取当天日期的最大时间
    public static String getDayEnd(String datetime) throws ParseException {
        if (CommonUtils.isNull(datetime)) {
            return null;
        }
        Date date = parseTimeStringToDate(datetime.substring(0, 10), YYYY_MM_DD);
        String dayEnd = datetime.substring(0, 10) + " 23:59:59";
        return dayEnd;
    }

    public static String getTomorrow() {
        Date date = new Date();//取时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 1);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime(); //这个时间就是日期往后推一天的结果
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }

    public static String getAfterDayDate(int len) {
        Date date = new Date();//取时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE, len);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime(); //这个时间就是日期往后推一天的结果
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }

}
